//
//  LoginViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/8.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "LoginViewController.h"
#import "MyViewController.h"

@interface LoginViewController ()<UIScrollViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIButton *btn1;//登录
@property (nonatomic,strong) UIButton *btn2;//忘记密码
@property (nonatomic,strong) UIButton *btn3;//商家版是什么
@property (nonatomic,strong) UITextField *phoneNumTextField;//账号框
@property (nonatomic,strong) UITextField *pswTextField;//密码框

@end

@implementation LoginViewController

-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [AppData share].doFirst = NO;
    [NSUserDefualManager storageValue:[NSNumber numberWithBool:YES] forKey:@"DoFirstLogin"];
    if (![AppData share].doFirst) {
        NSString *name = [NSUserDefualManager getValueByKey:UserName];
        _phoneNumTextField.text = name;
        NSLog(@"11111");
    }else {
        NSLog(@"22222");
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"" doBack:NO];
    self.navBar.alpha = 0;
    self.view.backgroundColor = SetColor(0, 140, 206, 1);
    
    UIImageView *adImageV = [[UIImageView alloc]initWithFrame:CGRectMake((ScreenWidth-187)/2, 95, 187, 60)];
    adImageV.image = [UIImage imageNamed:@"logo.png"];
    [self.view addSubview:adImageV];
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(35, 205, ScreenWidth-70, 42)];
    view1.backgroundColor = [UIColor clearColor];
    view1.layer.cornerRadius = 21;
    view1.layer.borderWidth = 1.5;
    view1.layer.borderColor = [UIColor whiteColor].CGColor;
    view1.layer.masksToBounds = YES;
    [self.view addSubview:view1];
    
    UIImageView *dlImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 12, 18, 18)];
    dlImageView1.image = [UIImage imageNamed:@"denglu42_42"];
    [view1 addSubview:dlImageView1];
    
    self.phoneNumTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 4, view1.frame.size.width-40, 34)];
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    dict1[NSForegroundColorAttributeName] = SetColor(148, 206, 235, 1);
    NSAttributedString *attribute1 = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:dict1];
    [self.phoneNumTextField setAttributedPlaceholder:attribute1];
    self.phoneNumTextField.tintColor =  SetColor(148, 206, 235, 1);
    self.phoneNumTextField.delegate = self;
    self.phoneNumTextField.tag = 100;
    self.phoneNumTextField.textAlignment = NSTextAlignmentLeft;
    self.phoneNumTextField.font = [UIFont systemFontOfSize:15];
    self.phoneNumTextField.textColor = [UIColor whiteColor];
    self.phoneNumTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view1 addSubview:self.phoneNumTextField];
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(35, 270, ScreenWidth-70, 42)];
    view2.backgroundColor = [UIColor clearColor];
    view2.layer.cornerRadius = 21;
    view2.layer.borderWidth = 1.5;
    view2.layer.borderColor = [UIColor whiteColor].CGColor;
    view2.layer.masksToBounds = YES;
    [self.view addSubview:view2];
    
    UIImageView *dlImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 11, 18, 20)];
    dlImageView2.image = [UIImage imageNamed:@"mima.png"];
    [view2 addSubview:dlImageView2];
    
    self.pswTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 4, view2.frame.size.width-40, 34)];
    self.pswTextField.delegate = self;
    self.pswTextField.tag = 200;
    self.pswTextField.textAlignment = NSTextAlignmentLeft;
    self.pswTextField.font = [UIFont systemFontOfSize:15];
    self.pswTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.pswTextField.secureTextEntry = YES;
    self.pswTextField.tintColor =  SetColor(148, 206, 235, 1);
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = SetColor(148, 206, 235, 1);
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:dict];
    [self.pswTextField setAttributedPlaceholder:attribute];
    
    self.pswTextField.textColor = [UIColor whiteColor];
    [view2 addSubview:self.pswTextField];
    
    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn1.tag = 100;
    self.btn1.layer.cornerRadius = 22.0;
    self.btn1.layer.masksToBounds = YES;
    self.btn1.backgroundColor = [UIColor whiteColor];
    self.btn1.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.btn1 setTitleColor:SetColor(0, 140, 206, 1) forState:UIControlStateNormal];
    self.btn1.frame = CGRectMake(35, 372, ScreenWidth-70, 44);
    [self.btn1 setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:self.btn1];
    [self.btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *bblabel = [[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight-30, ScreenWidth, 20)];
    bblabel.text = @"v1.0.0";
    bblabel.font = [UIFont systemFontOfSize:15];
    bblabel.textAlignment = NSTextAlignmentCenter;
    bblabel.textColor = SetColor(147, 205, 235, 1);
    [self.view addSubview:bblabel];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    if (ScreenHeight == 480) {
        CGPoint center = self.view.center;
        center.y = ScreenHeight/2-80;
        [UIView animateWithDuration:0.2 animations:^{
            self.view.center = center;
        }];
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (ScreenHeight == 480) {
        CGPoint center = self.view.center;
        center.y = ScreenHeight/2;
        [UIView animateWithDuration:0.2 animations:^{
            self.view.center = center;
        }];
    }
}
- (IBAction)tapHidentKB:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}
#pragma mark - 隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
-(void)click:(UIButton *)sender {
    [self.view endEditing:YES];
    [self login];
}
- (void)login {
    if (_phoneNumTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"账号不能为空" login:NO];
        return;
    }
    if (_pswTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"密码不能为空" login:NO];
        return;
    }
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *cid = [userDefaul objectForKey:@"cid"];
    NSString *kUrl = [NSString stringWithFormat:LZGloginUrl,_phoneNumTextField.text,_pswTextField.text,cid];
    [AFNetworkHandler GETWithAFNByURL:kUrl completion:^(id result) {
        NSDictionary *dic = result;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            NSDictionary *userMess = dic[@"d"];
            NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults]; // 蓝牙开关
            [userDefaul setObject:@"off" forKey:@"switch"];
            /***********懒掌柜****************/
            [AppData share].doLogin = YES;
            [AppData share].loginPhone = _phoneNumTextField.text;
            [AppData share].loginPsw = _pswTextField.text;
            [AppData share].username = userMess[@"username"];
            [AppData share].privatekey = userMess[@"privatekey"];
            [AppData share].shopname = userMess[@"shopname"];
            [AppData share].shoplogo = userMess[@"shoplogo"];
            
            [NSUserDefualManager storageValue:_phoneNumTextField.text forKey:UserName];
            [NSUserDefualManager storageValue:_pswTextField.text forKey:Psw];
            [NSUserDefualManager storageValue:@"1" forKey:@"doLogin"];
            [NSUserDefualManager storageValue:@"1" forKey:@"lingsheng"];
            [NSUserDefualManager storageValue:@"1" forKey:@"zhendong"];

            BaseTabBarViewController *baseTab = [[BaseTabBarViewController alloc] init];
            [baseTab changeBottomBtnWithIndex:0];
            [self presentViewController:baseTab animated:YES completion:nil];
        }else {
            [[MessShowView share] showWithMess:@"账户名或密码错误" login:NO];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
